$(function(){
	$("#loading-first").fadeOut(500);
	var type='add'
	{if $Think.get.maintenance_record_id}
	var data1={};
	var where_json={};
	where_json.maintenance_record_id=['in','{$Think.get.maintenance_record_id}'] ;
	data1.where_json=JSON.stringify(where_json);
	data1.pagenum=10;
	$.ajax({
		type:"post",
		url:"{:url('adminapi/Maintenancerecord/recordList')}",
		data:data1,
		success:function(res){
			console.log(res.list[0].service_img)
			$('.fileimg').attr('src',res.list[0].service_img)
		}
	})
	{/if}
	ajaxcar()
	//请求车辆
	function ajaxcar(){
	$.ajax({
		type:'post',
		url:'{:url("/unit/car/all")}',
		success:function(res){
			ajaxrepair()
			if(res.code==1){
				console.log(res)
				var carlist=`<option value=''>请选择车辆</option>`;
				for(var i=0;i<res.data.length;i++){
					if($('.car').data('carid')==res.data[i].car_id){
						carlist+=`<option selected value='${res.data[i].car_id}'>${res.data[i].number}</option>`
					}else{
						carlist+=`<option value='${res.data[i].car_id}'>${res.data[i].number}</option>`
					}
				}
				$('.car').html(carlist)
			}else{
				Err(res.msg)
			}
		},
		error:function(){
			Err('请求失败')
		}
	})
	}
	//请求维修厂
	function ajaxrepair(){
	$.ajax({
		type:'post',
		url:'{:url("/adminapi/Maintenance/maintenanceList")}',
		data:{
			fields:"maintenance_id,maintenance_name",
			page:1,
			pagenum:99999,
			where_json:JSON.stringify({company_id:$('.company_all_id').val()})
		},
		success:function(res){
			if(res.code==1){
				console.log(res)
				var carlist=`<option value=''>请选择维修厂</option>`;
				for(var i=0;i<res.list.length;i++){
					if($('.maintenance_id').data('maintenanceid')==res.list[i].maintenance_id){
						carlist+=`<option selected value='${res.list[i].maintenance_id}'>${res.list[i].maintenance_name}</option>`
					}else{
						carlist+=`<option value='${res.list[i].maintenance_id}'>${res.list[i].maintenance_name}</option>`
					}
				}
				$('.maintenance_id').html(carlist)
			}else{
				Err(res.msg)
			}
		},
		error:function(){
			Err('请求失败')
		}
	})
	}
	$('.keep').on('click',function(){
		var url='';
		var data={};
		var check=1;
		{if $Think.get.maintenance_record_id}
		url="{:url('adminapi/Maintenancerecord/edit')}"
		data.maintenance_record_id="{$Think.get.maintenance_record_id}"
		{else/}
		url="{:url('adminapi/Maintenancerecord/add')}"
		{/if}
		
		
		$('input').each(function(){
			if($(this).attr('name')){
				if($(this).attr('name')=='service_phone'){
					console.log($(this).val().length)
					if($(this).val().length<11){
						check=0;
						Err('电话不能少于11位，如果是固定电话，请加上区号')
					}
				}
				if($(this).data('check')){
					if($(this).val()){
						data[$(this).attr('name')]=$(this).val()
						
					}else{
						Err($(this).data('msg'))
						check=0;
					}
				}else{
					if($(this).val()){
						data[$(this).attr('name')]=$(this).val()
					}else{
						
					}
				}
			}
		});
		console.log(data)
		$('select').each(function(){
			if($(this).attr('name')){
				
				if($(this).data('check')){
					if($(this).val()){
						data[$(this).attr('name')]=$(this).val()
					}else{
						Err($(this).data('msg'))
						check=0;
					}
				}else{
					if($(this).val()){
						data[$(this).attr('name')]=$(this).val()
					}else{
						
					}
				}
			}
		})
		console.log(data)
		$('textarea').each(function(){
			data[$(this).attr('name')]=$(this).val()
		})
		data.company_id=$('.company_all_id').val()
		if($('.service_img').val()){
			var file = $('.service_img')[0].files[0]; 
			var formData = new FormData();
			var name = $('.service_img').val();
			formData.append("image",file);
			formData.append("name",name);
			$.ajax({
				type:"post",
				url:"{:url('adminapi/Upload/uploadImg')}",
				data:formData,
				processData : false, 
				contentType : false,
				success:function(res){
					console.log(res.image)
					data.service_img=res.image
				},
				error:function(){
					Err('请求失败')
				}
			});
		}
		if(check){
				Load()
				$.ajax({
					type:"post",
					url:url,
					data:data,
					success:function(res){
						Load(1)
						if(res.code==1){
							Msg('添加成功')
							
							{if $Think.get.maintenance_record_id}
							window.history.go(0)
							{else/}
							
							window.location.href="{:url('/unit/car/cost')}"
							{/if}
						}else{
							Err(res.msg)
						}
					},
					error:function(){
						Load(1)
						Err('请求失败')
					}
				});
			}
		console.log(data)
		
	});
	//点击添加图片
	$('button').on('click',function(){
		 	if($(this).data('type')=='file'){
		 		$(this).parent().find('input[type=file]').click()
		 	}
		});
		
	$('.back').on('click',function(){
		window.history.back()
	}); 
	$('.car_number').on('click',function(){
		choosecar("{:url('car/index',['op'=>'search'])}")
	});
	$('.service_type').val($('.service_type').data('idx'))
})